﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Windows;

namespace Songhay.Wpf.Mvvm
{
    using Songhay.Globalization;
    using Songhay.Models;

    /// <summary>
    /// Static members for WPF Applications
    /// </summary>
    public static class ApplicationUtility
    {
        /// <summary>
        /// Gets the Resource display text.
        /// </summary>
        /// <param name="text">The text.</param>
        public static string GetResourceDisplayText(string text)
        {
            text = Uri.UnescapeDataString(text)
                .Split('/').Last()
                .Replace(".s", string.Empty)
                .Replace(" - ", ": ");
            text = TextInfoUtility.ToTitleCase(text);

            return text;
        }

        /// <summary>
        /// Loads the resource.
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <returns>A <see cref="System.String"/> representation of the Resource.</returns>
        public static string LoadResource(Uri uri)
        {
            var appStream = Application.GetResourceStream(uri);
            using(var reader = new StreamReader(appStream.Stream))
            {
                var s = reader.ReadToEnd();
                return s;
            }
        }

        /// <summary>
        /// Lists the packed resources.
        /// </summary>
        /// <param name="dll">The DLL.</param>
        public static IEnumerable<PackedResource> ListPackedResources(Assembly dll)
        {
            var list = new List<PackedResource>();

            if(dll == null) return list;

            var simpleAsmName = new AssemblyName(dll.FullName).Name;
            var resourceName = simpleAsmName + ".g.resources";

            var getStream = new Func<Stream>(
            ()=>
            {
                var stream = dll.GetManifestResourceStream(resourceName);
                return stream;
            });

            using(var reader = new ResourceReader(getStream()))
            {
                var sb = new StringBuilder();
                foreach(DictionaryEntry entry in reader)
                {
                    var key = entry.Key.ToString();

                    if(key.StartsWith("packedxaml/"))
                    {
                        var resource =
                            new PackedResource
                            {
                                DisplayName = ApplicationUtility
                                    .GetResourceDisplayText(key),

                                Key = sb.AppendFormat(
                                    "/{0};Component/{1}",
                                        simpleAsmName, key).ToString()
                            };
                        list.Add(resource);
                        sb.Clear();
                    }
                }
            }

            return list.OrderBy(i => i.Key);
        }
    }
}
